<?php 
namespace Admin\Model;
use \Think\Model;


class BrandModel extends Model
{
	protected $_validate = array(
		array('brand_name','require','品牌名称不能为空','1'),
		array('brand_url','url','官网格式不正确',1),
		array('brand_url','require','官网不能为空',1),

		);
	public function getBrand(){

		return $this->select();
	}


	public function _before_insert(&$data,$option)
	{	

	$upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     C('IMG_RPATH'); // 设置附件上传根目录
    $upload->savePath  =     'Brand/'; // 设置附件上传（子）目录
    // 上传文件 
    $info   =   $upload->upload();

    if(!$info) {// 上传错误提示错误信息
        $this->error=$upload->getError();
    }	
    	$data['brand_logo'] = $info['brand_logo']['savepath'].$info['brand_logo']['savename'];
	}

	public function _before_delete($option)
	{
			$logo = $this->field('brand_logo')->find($option['where']['brand_id']);

			$rp = C('IMG_RPATH');

			unlink($rp.$logo['brand_logo']);
	}

	public function _before_update(&$data,$option)
	{		

			$upload  = new \Think\Upload();
			$upload->maxSize = 1024*1024*1024;  //3M
			$upload->exts = array('jpg','png','bmp','jpeg');
			$upload->rootPath = C('IMG_RPATH');
			$upload->savePath = 'Brand/';

			$info = $upload->upload();

			if(!info)
			{
				$this->error = $upload->getError();
			}
			else
			{
				//生成缩略图(省略..)
				
				//将上传文件的地址存到data数组中
				
				$saPath = $this->field('brand_logo')->find($option['where']['brand_id']);



				//删除在磁盘中的原图
				unlink(C('IMG_RPATH').$saPath['brand_logo']);
				$data['brand_logo'] = $info['brand_logo']['savepath'].$info['brand_logo']['savename'];

			}
	}
}

 ?>